package report

import java.util.Properties

import Configer.Configer
import org.apache.spark.sql.{SQLContext, SaveMode}
import org.apache.spark.{SparkConf, SparkContext}

//设备
object DeviceAnalysis {
  def main(args: Array[String]): Unit = {
    //sparkcontext
    val conf = new SparkConf().setMaster("local[*]").setAppName(s"${this.getClass.getName}")
      .set("spark.serializer",Configer.serializer)
    val sc = new SparkContext(conf)
    val sQLContext = new SQLContext(sc)
    //读取数据
    val dataFrame = sQLContext.read.parquet("E:\\小牛项目\\DMP广告项目34期\\资料PDF\\parquet")
    //处理数据
    dataFrame.registerTempTable("logs")
    val result = sQLContext.sql(
      """
        |select devicetype,
        |sum(case when requestmode=1 and processnode>=1 then 1 else 0 end) as allreq,
        |sum(case when requestmode=1 and processnode>=2 then 1 else 0 end) as effreq,
        |sum(case when requestmode=1 and processnode>=3 then 1 else 0 end) as adreq,
        |sum(case when iseffective=1 and isbilling=1 and isbid=1 and adorderid!=0 then 1 else 0 end) as adjoin,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then 1 else 0 end) as adsucess,
        |sum(case when requestmode=2 and iseffective=1 then 1 else 0 end) as adshow,
        |sum(case when requestmode=3 and iseffective=1 then 1 else 0 end) as adclick,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then adpayment/1000 else 0 end) as adpay,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then winprice/1000 else 0 end) as adwin
        |from logs
        |group by devicetype
      """.stripMargin)
    //存储数据
    val props = new Properties()
    props.setProperty("driver",Configer.driver)
    props.setProperty("user",Configer.user)
    props.setProperty("password",Configer.password)
    result.write.mode(SaveMode.Overwrite).jdbc(Configer.url,"Area34",props)

    sc.stop()
  }
}
